﻿
Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Product_Detail
    Inherits System.Web.UI.UserControl
    

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            If Not Page.RouteData.Values("ProductId") Is Nothing Then

                hidProductID.Value = Page.RouteData.Values("ProductId")
                LoadData(hidProductID.Value)
            End If
            
        End If
    End Sub

    Private Sub LoadData(ByVal intProductId As Integer)
        Using Data As New VSEntities
            Dim p = (From q In Data.Products Where q.ProductId = intProductId _
                      Select q).FirstOrDefault
            If Not p Is Nothing Then

                Page.Title = p.Title & " | " & DomainName
                Page.MetaDescription = p.Description.Replace("\n", " ")

                ltrCode.Text = p.Code
                ltrTitle.Text = p.Title

                ltrPrice.Text = FormatNumber(p.SellPrice.ToString())
                If p.IsSellOff = True Then
                    lblOriginalPrice.Text = FormatNumber(p.OriginalPrice.ToString())
                End If


                ltrThongTinChung.Text = p.Description

                If p.TinhTrangHang = 0 Then
                    ltrTinhTrang.Text = " còn hàng"
                ElseIf p.TinhTrangHang = 1 Then
                    ltrTinhTrang.Text = " hết hàng"
                    btnAddCart.Enabled = False
                Else
                    ltrTinhTrang.Text = " ngừng bán"
                    btnAddCart.Enabled = False
                End If
                Dim h = (From f In Data.ProductImages Where f.ProductId = p.ProductId Select f.ImageURL, f.Description).ToList
                If h.Count > 0 Then
                    rptBigBanner.DataSource = h
                    rptBigBanner.DataBind()
                    rptImageThumb.DataSource = h
                    rptImageThumb.DataBind()
                Else
                    Dim k = (From q In Data.Products Where q.ProductId = p.ProductId Select New With {.ImageURL = q.ImagePreview, .Description = q.Description}).ToList
                    rptBigBanner.DataSource = k
                    rptBigBanner.DataBind()
                    rptImageThumb.DataSource = k
                    rptImageThumb.DataBind()
                End If



                hidCateId.Value = p.CateID

                BindToGrid(1)
                'bind san pham cung cat
                'Dim k = (From q In Data.Products Where q.ProductId <> p.ProductId And q.CateID = q.CateID And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
                '             Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(15).ToList
                'rptShow.DataSource = k
                'rptShow.DataBind()


            End If
        End Using
    End Sub

    Protected Sub rptImageThumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImageThumb.ItemDataBound
        Dim row As Object = e.Item.DataItem
        Dim imgThumb As Image = e.Item.FindControl("imgThumb")
        imgThumb.ImageUrl = CookedImageURL("90-60", row.ImageURL)
        imgThumb.AlternateText = row.Description
        imgThumb.Style.Add("overflow", "hidden")

    End Sub

    Protected Sub rptBigBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBigBanner.ItemDataBound
        Dim row As Object = e.Item.DataItem
        Dim HighslideImage1 As Encosia.HighslideImage = e.Item.FindControl("HighslideImage1")
        HighslideImage1.ImageUrl = CookedImageURL("320-300", row.ImageURL)
        HighslideImage1.FullImageURL = CookedImageURL("800-600", row.ImageURL)
        HighslideImage1.AlternateText = row.Description
        If e.Item.ItemIndex = 0 Then
            HighslideImage1.Attributes.Add("style", "display:inline")
        Else
            HighslideImage1.Attributes.Add("style", "display:none")
        End If

    End Sub


    Private Sub BindToGrid(ByVal ipage As Integer)
        Using Data As New VSEntities

            hplCate.Text = (From q In Data.Categories Where q.CatId = hidCateId.Value Select q.Title).FirstOrDefault

            Dim a = (From r In Data.uspSelectSubCate(hidCateId.Value) Select r.CateID).ToArray

            Dim p = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending Select q).Skip((ipage - 1) * 12).Take(12).ToList

            Dim count = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending Select q.ProductId).Count

            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 12, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
                results_resultsNavigationDiv.Visible = False
            End If

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row = CType(e.Item.DataItem, Product)
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim hplTitleImage As HyperLink = CType(e.Item.FindControl("hplTitleImage"), HyperLink)
        img.ImageUrl = CookedImageURL("212-180", row.ImagePreview)
        img.AlternateText = row.Title
        img.Style.Add("overflow", "hidden")
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
        hplTitleImage.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        Dim ltrSaleOff As Literal = CType(e.Item.FindControl("ltrSaleOff"), Literal)
        If row.IsSellOff = True Then
            ltrSaleOff.Text = "<span class=""SellOff"">" & FormatNumber(row.OriginalPrice) & "</span>"
        End If

        ltrPrice.Text = FormatNumber(row.SellPrice.ToString())
    End Sub

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub


    Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCart.Click

        AddShoppingCart(hidProductID.Value)
        Session("LastURL") = Request.Url
        Response.Redirect(MainDomain & "gio-hang.html")


    End Sub

    Private Sub AddShoppingCart(ByVal _ProductID As String)



        Using Data As New VSEntities
            Dim _cartID As String = Session("CartId")
            Dim check = (From q In Data.ShoppingCarts Where q.CartId = _cartID And q.ProductId = _ProductID Select q).FirstOrDefault
            If check Is Nothing Then
                Dim p As New ShoppingCart
                p.CartId = _cartID
                p.ProductId = _ProductID
                p.UserId = Session("UserId")
                ' p.StoreId = (From q In Data.Products Where q.ProductId = _ProductID Select q.StoreId).FirstOrDefault
                p.Quantity = 1
                Data.ShoppingCarts.AddObject(p)
                Data.SaveChanges()

            End If

        End Using


    End Sub

End Class
